package com.snj.bestdealer;

import java.util.ArrayList;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ViewFlipper;

import com.snj.bestdealer.main.MainActivity;

public class GuestActivity extends InjectViewActivity implements OnClickListener {
	
	private int m_nPreTouchPosX = 0;
	
	public static ArrayList<Bitmap> vpadimgarr = new ArrayList<Bitmap>();
	
	@InjectView
	ViewFlipper vf_guestad;
	
	@InjectView
	Button btn_gueststart;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_guest);
		super.onCreate(savedInstanceState);
		
		for(int i=0; i<vpadimgarr.size(); i++)
		{
			ImageView img_ad = new ImageView(GuestActivity.this);
			img_ad.setImageBitmap(vpadimgarr.get(i));
			ViewGroup.LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			
			params.width = LayoutParams.MATCH_PARENT;
			params.height = LayoutParams.MATCH_PARENT;
			
			img_ad.setLayoutParams(params);
			
			vf_guestad.addView(img_ad);
			vf_guestad.setOnTouchListener(MyTouchListener);
		}
		
		btn_gueststart.setOnClickListener(this);
	}
	
	View.OnTouchListener MyTouchListener = new View.OnTouchListener()
    {
    	public boolean onTouch(View v, MotionEvent event) 
    	{
    		if (event.getAction() == MotionEvent.ACTION_DOWN)
    		{
    			m_nPreTouchPosX = (int)event.getX();
    		}
    		
    		if (event.getAction() == MotionEvent.ACTION_UP)
    		{
    			int nTouchPosX = (int)event.getX();
    			
    			if (nTouchPosX < m_nPreTouchPosX)
    			{
    				MoveNextView();
    			}
    			else if (nTouchPosX > m_nPreTouchPosX)
    			{
    				MovewPreviousView();
    			}
    			
    			m_nPreTouchPosX = nTouchPosX;
    		}
    		
            return true;
        }
    };
    
    private void MoveNextView()
    {	
		vf_guestad.setInAnimation(AnimationUtils.loadAnimation(GuestActivity.this,	R.anim.appear_from_right));
		vf_guestad.setOutAnimation(AnimationUtils.loadAnimation(GuestActivity.this, R.anim.disappear_to_left));
		vf_guestad.showNext();
		
		int currentindex = vf_guestad.getDisplayedChild();
		
		/*
		for(int i=0; i<5; i++)
		{
			if(currentindex == i)
			{
				img_eventcovers[i].setImageResource(R.drawable.point_on);
			}
			else
			{
				img_eventcovers[i].setImageResource(R.drawable.point_off);
			}
		}*/
    }
    
    private void MovewPreviousView()
    {    	
    	
    	vf_guestad.setInAnimation(AnimationUtils.loadAnimation(GuestActivity.this,	R.anim.appear_from_left));
    	vf_guestad.setOutAnimation(AnimationUtils.loadAnimation(GuestActivity.this, R.anim.disappear_to_right));
    	vf_guestad.showPrevious();
    	
    	int currentindex = vf_guestad.getDisplayedChild();
		
    	/*
		for(int i=0; i<5; i++)
		{
			if(currentindex == i)
			{
				img_eventcovers[i].setImageResource(R.drawable.point_on);
			}
			else
			{
				img_eventcovers[i].setImageResource(R.drawable.point_off);
			}
		}*/
    }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		if(v.equals(btn_gueststart))
		{
			startActivity(new Intent(getApplicationContext(), MainActivity.class));
			finish();
		}
	}
	
}
